package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxiangrice.pojo.*;
import com.yangxiangrice.service.AgItemsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月17日 20:39
 */
@Controller
@RequestMapping("/agitems")
@CrossOrigin
@Slf4j
public class AgItemsController {
    @Autowired
    AgItemsService agItemsService;

    @GetMapping(value = "/getagitems")
    @ResponseBody
    public Result getseed() {
        Result<List> result = new Result<>();
        Option option = null;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parentAgItemId", 0);
        List<Option> res = new ArrayList<>();
        List<AgItems> list = agItemsService.list(wrapper);
        option = new Option();
        option.setLabel("如无父类选此项");
        option.setValue("-1");
        res.add(option);
        for (AgItems agItems : list) {
            option = new Option();
            option.setLabel(agItems.getAgItemName());
            option.setValue(agItems.getAgItemId().toString());
            res.add(option);
        }
        return result.ok(res);
    }

    @GetMapping(value = "/getparent")
    @ResponseBody
    public Result getparent() {
        Result<List> result = new Result<>();
        FilterOption option = null;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parentAgItemId", 0);
        List<FilterOption> res = new ArrayList<>();
        List<AgItems> list = agItemsService.list(wrapper);
        for (AgItems agItems : list) {
            option = new FilterOption();
            option.setText(agItems.getAgItemName());
            option.setValue(agItems.getAgItemId().toString());
            res.add(option);
        }
        return result.ok(res);
    }

    @GetMapping(value = "/listpage")
    @ResponseBody
    public Result listpage(@RequestParam("page") int page,
                           @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        IPage<AgItems> agItemsIPage = agItemsService.seletePage(new Page<>(page, limit), null);
        return result.ok(agItemsIPage);
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody AgItems agItems) {
        Result<Boolean> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("agItemName", agItems.getAgItemName());
        if(ObjectUtil.isEmpty(agItems.getAgItemId())){
            if (agItemsService.count(wrapper) > 0) {
                result.fail("农资名称已存在，请重新输入");
            }else{
                agItems.setCreatetime(new Timestamp(System.currentTimeMillis()));
                if (ObjectUtil.isEmpty(agItems.getParentAgItemId())) {
                    agItems.setParentAgItemId(0);
                }
                boolean tag = agItemsService.save(agItems);
                if (tag) {
                    result.ok(tag);
                }else {
                    result.fail("添加失败，请重试");
                }
            }
        }else{
            if (agItemsService.count(wrapper) > 0) {
                result.fail("农资名称已存在，请重新输入");
            }else{
                agItems.setCreatetime(new Timestamp(System.currentTimeMillis()));
                boolean tag = agItemsService.updateById(agItems);
                if (tag) {
                    result.ok(tag);
                }else {
                    result.fail("更新失败，请重试");
                }
            }
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam(value = "agItemId") Integer agItemId) {
        Result<Boolean> result = new Result<>();
        boolean tag = agItemsService.removeById(agItemId);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody int[] ids) {
        ArrayList<Integer> list = new ArrayList<>();
        Result<Boolean> result = new Result<>();
        for (int id : ids) {
            list.add(id);
        }
        boolean tag = agItemsService.removeByIds(list);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public Result search(@RequestParam("input") String input,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("agItemName",input);
        IPage<AgItems> agItemsIPage = agItemsService.seletePage(new Page<>(page, limit), wrapper);
        if (ObjectUtil.isEmpty(agItemsIPage)) {
            result.fail("未找到符合条件的数据");
        }else{
            result.ok(agItemsIPage);
        }
        return result;
    }
}
